package com.wenheng.controller;

import com.alibaba.fastjson.JSONObject;
import com.wenheng.client.AdminClient;
import com.wenheng.client.CommodityClient;
import com.wenheng.service.OrderService;
import com.wenheng.vo.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Auther: 文恒
 * @Date: 2021/3/26 - 03 - 26 -10:44
 * @Description:com.wenheng.controller
 * @version:1.0
 */
@RestController
public class OrderController {
    @Autowired
    private CommodityClient commodityClient;
    @Autowired
    private OrderService orderService;
    @Autowired
    private AdminClient adminClient;
    //生成订单    参数为 （隐藏参数：地址id） 商品id  购买商品数量
    @PostMapping("/order/createOrder")
    public String createOrder(@RequestParam("addresssId")Integer addresssId
            , @RequestParam("productId") Integer productId
            , @RequestParam("productNum") Integer productNum
            ,@RequestParam("token") String token)
    {
        EasybuyOrder easybuyOrder = new EasybuyOrder();        //将订单信息封装
        //用户信息从redis中获取
        System.out.println(token);
        String userInfo =  adminClient.adminInfo(token);
        EasybuyUser easybuyUser = JSONObject.parseObject(userInfo,EasybuyUser.class);
            //用户选择的地址
        System.out.println("地址："+adminClient.adminAddressInfo(addresssId));
        List<EasybuyUserAddress> list = JSONObject.parseArray(adminClient.adminAddressInfo(addresssId),EasybuyUserAddress.class);
        for (EasybuyUserAddress address : list) {  //用户地址封装
            easybuyOrder.setUseraddress(address.getAddress());
        }
        // 获取商品信息
        Map<String,Object> map = commodityClient.findSeckill(productId);
        System.out.println(map.get("data"));
        String json = JSONObject.toJSONString(map.get("data"));
        System.out.println(json);
        EasybuyProduct easybuyProduct =JSONObject.parseObject(json,EasybuyProduct.class);
        System.out.println(easybuyProduct);
        //开始封装    其他信息封装
        easybuyOrder.setLoginname(easybuyUser.getLoginname());
        easybuyOrder.setUserid(easybuyUser.getId());
        easybuyOrder.setCreatetime(new Date());
        easybuyOrder.setCost(easybuyProduct.getPrice() * productNum);
        //生成订单号
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        String newDate=sdf.format(new Date());
        easybuyOrder.setSerialnumber(UUID.randomUUID().toString()+newDate); //最后记得加一个用户的id

        return orderService.createOrderInfo(easybuyOrder,productId,productNum,easybuyOrder.getCost());
    }

    //查询对应的订单信息
    @GetMapping("/order/findOrderInfo")
    public Map<String,Object> findOrderInfo(@RequestParam("orderNum") String orderNum){
        Map<String,Object> map = new HashMap<>();
        //查询出订单信息
        List<EasybuyOrder> list = orderService.findOrderInfo(orderNum);
        String orderId = null;
        for (EasybuyOrder easybuyOrder : list) {
            orderId = easybuyOrder.getId().toString();
        }
        map.put("orderInfo",list);
        //订单下的商品信息
        List<EasybuyOrderDetail> list2 = orderService.findOrder(orderId);
        map.put("order",list2);
        //查询出具体商品
        for (EasybuyOrderDetail easybuyOrderDetail : list2) {
           map.put("product",commodityClient.findSeckill(easybuyOrderDetail.getProductid()));
        }
        return map;
    }

}
